/**
 * @(#)Client.java
 *
 * Copyright: Copyright (c) 2003 Carnegie Mellon University
 *
 * This program is the implementation of the original system in the Assignment 2 of "Architectures
 * of Software Systems" class. For the details of each classes, please refer to the provided javadoc
 * documents. You will also find some useful information such as implementation decisions, coding
 * standards, and naming conventions in the README file.
 */


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;


/**
 * This class represents a client node in the client tier which is responsible for getting
 * user input, executing corresponding methods in a logic node, and displaying returned
 * messages from a logic node. Returned messages come as string objects.
 *
 * @author Jung Soo Kim
 * @version 1.0
 */
public class Client {

    private static final String CLIENT_NAME = "Client";
    private static final String LOGIC_NAME  = "Logic";
    private static final String SECURITY_NAME  = "Security";

    /**
     * Remote reference to the logic node.
     */
    protected RILogic rmiLogicNode;
    protected RISecurity rmiSecurityNode;
    

    /**
     * Constructs a client. A remote reference to a logic node is acquired at the time of
     * creation.
     *
     * @param sLogicName the name of logic node
     */
    /*
    public Client(String sLogicName)
           throws NotBoundException, MalformedURLException, RemoteException {
        // Get the remote reference to logic node.
    	System.out.println("Client Const param : " + sLogicName);
        this.rmiLogicNode = (RILogic) Naming.lookup(sLogicName);
    }
    */
    
    public Client(String sSecurityName)
            throws NotBoundException, MalformedURLException, RemoteException {
         // Get the remote reference to logic node.
    	System.out.println("Client Const param : " + sSecurityName);
         this.rmiSecurityNode = (RISecurity) Naming.lookup(sSecurityName);
     }
	
    /**
     * The body of client. It continuously gets user input and executes associated RMI methods
     * in the logic node.
     */
    public void execute()
                throws RemoteException, IOException {
        // Create a buffered reader using system input stream.
        BufferedReader objReader = new BufferedReader(new InputStreamReader(System.in));

        while (true) {
            // Show available commands and get a choice.
            System.out.println("\nAssignment 2 - Student Registration System\n");
            System.out.println("0) Authenticate");
            System.out.println("1) List all students");
            System.out.println("2) List all courses");
            System.out.println("3) List students who registered for a course");
            System.out.println("4) List courses a student has registered for");
            System.out.println("5) List courses a student has completed");
            System.out.println("6) Register a student for a course");
            System.out.println("x) Exit");
            System.out.println("\nEnter your choice and press return >> ");
            String sChoice = objReader.readLine().trim();
            
            if (sChoice.equals("0")) {
                // Get course ID and course section from user.
                System.out.println("\nEnter User ID and press return >> ");
                String sUserID = objReader.readLine().trim();
                System.out.println("\nEnter Password and press return >> ");
                String sPassword = objReader.readLine().trim();

                System.out.println("input id : " + sUserID + ", pwd : " + sPassword);
                // Execute RMI method to get the student information and show it.
                boolean isValid = this.rmiSecurityNode.checkIDPwd(sUserID, sPassword);
                
                System.out.println("Result : " + isValid);
                
                if (isValid)
                	System.out.println("Login is success.");
                
                else
                	System.out.println("Login is fail..");
                
                continue;
            }

            // Execute command 1: List all students.
            if (sChoice.equals("1")) {
                // Execute RMI method to get the student information and show it.
                //System.out.println("\n" + this.rmiLogicNode.getAllStudents());
            	System.out.println("\n" + this.rmiSecurityNode.getAllStudents());
                continue; 
            }

            // Execute command 2: List all courses.
            if (sChoice.equals("2")) {
                // Execute RMI method to get the course information and show it.
                System.out.println("\n" + this.rmiSecurityNode.getAllCourses());
                continue;
            }

            // Execute command 3: List students registered for a course.
            if (sChoice.equals("3")) {
                // Get course ID and course section from user.
                System.out.println("\nEnter course ID and press return >> ");
                String sCID = objReader.readLine().trim();
                System.out.println("\nEnter course section and press return >> ");
                String sSection = objReader.readLine().trim();

                // Execute RMI method to get the student information and show it.
                System.out.println("\n" + this.rmiSecurityNode.getRegisteredStudents(sCID, sSection));
                continue;
            }

            // Execute command 4: List courses a student has registered for.
            if (sChoice.equals("4")) {
                // Get student ID from user.
                System.out.println("\nEnter student ID and press return >> ");
                String sSID = objReader.readLine().trim();

                // Execute RMI method to get the course information and show it.
                System.out.println("\n" + this.rmiSecurityNode.getRegisteredCourses(sSID));
                continue;
            }

            // Execute command 5: List courses a student has completed.
            if (sChoice.equals("5")) {
                // Get student ID from user.
                System.out.println("\nEnter student ID and press return >> ");
                String sSID = objReader.readLine().trim();

                // Execute RMI method to get the course information and show it.
                System.out.println("\n" + this.rmiSecurityNode.getCompletedCourses(sSID));
                continue;
            }

            // Execute command 6: Register a student for a course.
            if (sChoice.equals("6")) {
                // Get student ID, course ID, and course section from user.
                System.out.println("\nEnter student ID and press return >> ");
                String sSID = objReader.readLine().trim();
                System.out.println("\nEnter course ID and press return >> ");
                String sCID = objReader.readLine().trim();
                System.out.println("\nEnter course section and press return >> ");
                String sSection = objReader.readLine().trim();

                // Execute RMI method to make a new registration and show the result.
                System.out.println("\n" + this.rmiSecurityNode.makeARegistration(sSID, sCID, sSection));
                continue;
            }

            // Terminate this client.
            if (sChoice.equalsIgnoreCase("X")) {
                break;
            }
        }

        // Clean up the resources.
        objReader.close();
    }

    /**
     * Creates a client and starts it. Two parameters are expected, the first one as logic
     * name and the second one as client name.
     *
     * @param args array of input parameters
     */
    public static void main(String args[]) {
        // Check the number of parameters.
        if (args.length != 0) {
            System.out.println("Incorrect number of parameters");
            System.out.println("Usage: java Client");
            System.exit(1);
        }

        try {
            // Create a client and execute it.
            //Client objClient = new Client(LOGIC_NAME);
        	Client objClient = new Client(SECURITY_NAME);
            objClient.execute();
        }
        catch (java.rmi.ConnectException e) {
            // Show error message and quit.
            System.err.println("Java RMI error: check if rmiregistry is running.");
            System.exit(1);
	}
        catch (java.rmi.NotBoundException e) {
            // Show error message and quit.
            System.err.println("Java RMI error: check if logic node is running.");
            System.exit(1);
        }

        catch (Exception e) {
            // Dump the exception information for debugging.
            System.out.println("Unexpected exception at " + CLIENT_NAME);
            e.printStackTrace();
            System.exit(1);
        }
    }
}
